package com.yunxin.core.infra.persistence.repository.user;

import com.yunxin.core.domain.model.user.UserAuth;
import com.yunxin.core.domain.model.user.UserAuthRepository;
import com.yunxin.core.infra.persistence.converter.UserInfoConverter;
import com.yunxin.core.infra.persistence.entity.user.UserAuthDO;
import com.yunxin.core.infra.persistence.mapper.user.UserAuthExtendMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

/**
 * @author zero°
 * @since 2023-07-13
 */
@Repository
@RequiredArgsConstructor
public class UserAuthRepositoryImpl implements UserAuthRepository {

    private final UserAuthExtendMapper userAuthExtendMapper;

    private final UserInfoConverter userInfoConverter;

    @Override
    public boolean saveUserAuth(UserAuth userAuth) {
        UserAuthDO userAuthDO = userInfoConverter.formUserAuth(userAuth);
        int count = userAuthExtendMapper.insertSelective(userAuthDO);
        return count > 0;
    }

    @Override
    public boolean updateUserAuth(UserAuth userAuth) {
        UserAuthDO userAuthDO = userInfoConverter.formUserAuth(userAuth);
        int count = userAuthExtendMapper.updateByPrimaryKeySelective(userAuthDO);
        return count > 0;
    }

    @Override
    public UserAuth getUserAuthByIdentifierMd5(String identifierMd5) {
        UserAuthDO userAuthDO = userAuthExtendMapper.selectUserAuthByIdentifierMd5(identifierMd5);
        return userInfoConverter.toUserAuth(userAuthDO);
    }

    @Override
    public UserAuth getUserAuthByIdentifier(String identifier, String authSubject) {
        UserAuthDO userAuthDO = userAuthExtendMapper.selectUserAuthByIdentifier(identifier, authSubject);
        return userInfoConverter.toUserAuth(userAuthDO);
    }

    /**
     * 根据用户code获取MD5唯一标识
     * @param userCode
     * @return
     */
    @Override
    public UserAuth getVisitorIdByUserCode(String userCode) {
        return userAuthExtendMapper.getVisitorByUserCode(userCode);
    }
}
